package fileModel;

import java.io.File;
import java.io.IOException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class Tag /*implements Runnable*/{
	
	private String event;
	private String path;
	private File file;

	//The constructor.
	public Tag (String event, String path, File file){
		this.event=event;
		this.path=path;
		this.file=file;
	}
	
	/**
	 * The method tag the file, and update the table TAGS of the database.   
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public void tagTheFile() throws ClassNotFoundException, IOException{
		
		System.out.println("In tagTheFile");
		
		// File file = new File("java.pdf");
	 
  /*      FileInputStream fis = new FileInputStream(file);
        //System.out.println(file.exists() + "!!");
        //InputStream in = resource.openStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); //no doubt here is 0
                //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
               // System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
           // Logger.getLogger(genJpeg.class.getName()).log(Level.SEVERE, null, ex);
        }
        byte[] bytes = bos.toByteArray();
        
        long hashCode = bytes.hashCode();
        
	    // System.out.println("bytes.hashCode() = "+hashCode);
	     
	     bos.flush();
	     bos.close();*/
	     
	     
	     
 
        //below is the different part
 /*       File someFile = new File("java2.pdf");
        FileOutputStream fos = new FileOutputStream(someFile);
        fos.write(bytes);
        fos.flush();
        fos.close();*/

		
		
		
		//Create a 
		//File theFile = new File(path);
		
		//Create an object that represent a row at the table tags.
		TagObj tagFile = new TagObj();
		
		//Set the column File.
	//	tagFile.setHashCode(hashCode);
		//Set the column Tag.
		tagFile.setTag(String.valueOf(file.length()));
		//Set the column Path.
		tagFile.setPath(path);
		
		System.out.println("String.valueOf(file.length()) = "+ String.valueOf(file.length())+"\n path = "+path);
		
		//Create a session.
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
		Session session = sessionFactory.openSession();
		TagSession theTagSession = new TagSession(session);
		System.out.println("-------------------The event = "+event);
		if(event.equals("ENTRY_CREATE")){
			
			//Save the new tag at the table.
			theTagSession.saveTag(tagFile);
			//System.out.println("------------------bytes.hashCode() = "+hashCode);
			System.out.println("------------------tagFile saved with tag = "+ tagFile.getTag());
			
		}
		else if(event.equals("ENTRY_DELETE")){
			
			//Delete the new tag at the table.
			theTagSession.deleteTag(tagFile);
			//System.out.println("------------------bytes.hashCode() = "+hashCode);
			System.out.println("------------------tagFile deleted with tag = "+ tagFile.getTag());
			
		}
		
		
		session.flush();
		
	}
	
	
	
	/*public void run() {
		
		
	}*/

}
